//
//  VisitorView.swift
//  Residual_mem
//
//  Created by 武振磊 on 16/7/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class VisitorView: UIView {

    //MARK: 构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //根据控制器定制画面
    func setUpInfoo(imageName: String?, title: String){
        
        titleLabel.text = title
        
        // 如果没有就表明是首页
        guard let imageName = imageName else{

            startAnim()
            return
        }
        houseImage.hidden = true
        maskIconImage.hidden = true
        iconImage.image = UIImage(named: imageName)
    }
    
    func startAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //保证动画不会被释放
        anim.removedOnCompletion = false
        
        iconImage.layer .addAnimation(anim, forKey: nil)
    }
    
    // MARK: 懒加载控件
    // 圆圈
    private lazy var iconImage:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮盖
    private lazy var maskIconImage:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var houseImage:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //标签
    private lazy var titleLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    //注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable", fontSize: 14)
    //登录按钮
    private lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable", fontSize: 14)
}

//MARK: 定制画面
extension VisitorView{
    private func setUpUI(){
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        // 圆圈
        addSubview(iconImage)
        //遮盖
        addSubview(maskIconImage)
        //小房子
        addSubview(houseImage)
        //标签
        addSubview(titleLabel)
        //注册按钮
        addSubview(registerButton)
        //登录按钮
        addSubview(loginButton)
        
        
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 圆圈
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        //遮盖
        addConstraint(NSLayoutConstraint(item: maskIconImage, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconImage, attribute: NSLayoutAttribute.Right, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconImage, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconImage, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        //小房子
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //标签
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: registerButton, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: .Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
    }
}
